# 爬网易云薛之谦全部歌曲
import requests
import json


class Music_spider(object):  # 只能爬到一页的数据
    def __init__(self):
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'}
        self.data = {  #
            'params': 'ltMgoCFtuuAXtbWrk3hTlCKVo9p7FQuuoXf1quW2CEnthVbyutcb+YTYSt1xJePR8LAaphpJcMh4CGQIsWhBrSiq82Ov5BjXUEz2VLSs05IDwbCP/XtWUSOe/+hR2qiu3ZHys4GNxv5Qz7DS1VVuugstZIxBm5CBGzarlQfd1sF+j8QryJcNUo4awrWdBZglLhrV88K0qj5G10hpkPMSJmPHSE86d7fKqcia8UUNvqOfkj0ELk6zBBnhMnlAGnMg42q6V1v8g76sLb3wgG4flA==',
            'encSecKey': '00b5a797b8ac609c36b99533999331dbba9b9ca26e8e423a2954817925cca6d61fdda8eb1c595113fe0dca4f7d3e5f66d099deb283bd995ca8fe9a46f4a80ed56a67e807730abb24d06cb48f59a0027f2dc249779b3891f7a09a3b54c8c8070ccd0cb144634a78bb62274dd93decedddf9fce179c43e3d8511dca59cb33911a3'
        }

    def parse_url(self, url):  # 获取源码
        response = requests.post(url=url, headers=self.headers, data=self.data)
        return response.json()

    def save_json(self, parse_str):  # 保存源码
        with open('薛之谦json源码.json', 'a')as f:
            f.write(json.dumps(parse_str, ensure_ascii=False, indent=2))

    def download(self, parse_str):  # 下载歌曲
        title = parse_str['result']['songs']
        for i in title:
            name = i.get('name')
            id = i.get('id')
            url = 'http://music.163.com/song/media/outer/url?id={}.mp3'.format(id)
            print("正在下载:{}".format(name))
            with open(name + ".mp3", 'wb')as f:
                f.write(requests.get(url=url, headers=self.headers).content)

    def run(self):
        url = 'https://music.163.com/weapi/cloudsearch/get/web?csrf_token='
        parse_str = self.parse_url(url)
        self.download(parse_str)
        self.save_json(parse_str)


if __name__ == '__main__':
    music = Music_spider()
    music.run()
